﻿mainModule.factory("itemSvc", ["$http", "notification", "cache", "$q", "articleSvc", function ($http, notification, cache, $q, articleSvc) {
    var article;
    var columnsList, columnsMap;
    var columnDefinitions, i;    

    function getColumns(articleID) {
        return articleSvc.findById(articleID).then(function (response) {
            columnsList = [], columnsMap = [];

            columnDefinitions = response.data.Columns;
            article = response.data;
            for (i = 0; i < columnDefinitions.length; i++) {
                columnsList.push(columnDefinitions[i].Name);
                columnsMap[columnDefinitions[i].ID] = columnDefinitions[i].Name;
            }
        });
    }    

    function createDynamicList(list) {
        var i, item, dynamicList = [], dynamicItem, lastItem = {};
        for (i = 0 ; i < list.length; i++) {
            item = list[i];
            if (item.ItemID !== lastItem.ItemID) {
                dynamicItem = {};
                lastItem = item;
                dynamicItem.ItemID = item.ItemID;
                dynamicItem[columnsMap[item.ColumnID]] = item.Value;
                dynamicList.push(dynamicItem);
            }
            else {
                dynamicItem[columnsMap[item.ColumnID]] = item.Value;
            }
        }

        return dynamicList;
    }

    function createDynamicItem(item) {
        var i, dynamicItem, itemValue;
        dynamicItem = angular.extend({}, dynamicItem, item);
        delete dynamicItem.DataItems;
        
        for (i = 0 ; i < item.DataItems.length; i++) {
                itemValue = item.DataItems[i];                
                dynamicItem[columnsMap[itemValue.ColumnID]] = itemValue.Value;
        }

        return dynamicItem;
    }
    
    return {
        createDynamicItem : createDynamicItem,
        dynamicFindBy: function (query) {
            var that = this;
            var deferred = $q.defer();

            var columnsListPromise = getColumns(query.ArticleID);           
            columnsListPromise.then(function () {
                query.ColumnsCount = columnsList.length;
                that.tabularFindBy(query).then(function (response) {
                    response.data.List = createDynamicList(response.data.List);
                    response.data.ColumnsList = columnsList;
                    deferred.resolve(response);
                });
            });

            return deferred.promise;
        },
        tabularFindBy: function (query, article) {
                var promise = $http.post('/api/itemapi/tfindby', { Value: query })
                promise.catch(function (ex) { notification.notify(ex) });                                                    
                return promise;
        },
        simpleFindBy: function (query, article) {
            var promise = $http.post('/api/itemapi/sfindby', { Value: query })
            promise.catch(function (ex) { notification.notify(ex) });
            return promise;
        },
        findById: function (id) {
            return $http.post('/api/itemapi/findbyid/' + id).catch(function (ex) { notification.notify(ex) });
        },

        edit: function (id, item) {
            return $http.put('/api/itemapi/' + id, { Value: item }).catch(function (ex) { notification.notify(ex) });
        },

        add: function (item) {
            return $http.post('/api/itemapi/', { Value: item }).catch(function (ex) { notification.notify(ex) });
        },

        remove : function (id) {
            return $http.delete('/api/itemapi/' + id).catch(function (ex) { notification.notify(ex) });
        }
    }
}]);